package org.csu.mypetstore.controller;

import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.domain.Category;
import org.csu.mypetstore.domain.Item;
import org.csu.mypetstore.domain.Product;
import org.csu.mypetstore.service.CatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

@Controller
@RequestMapping("/seller")
public class SellerController {

    @Autowired
    Account account;

    @Autowired
    CatalogService catalogService;

    @GetMapping("/exit")
    public String exit(){
        return "catalog/main";
    }

    @GetMapping("/viewSellerEntry")
    public String viewSellerEntry(){
        return "seller/sellerEntry";
    }

    @PostMapping("/sellerEntry")
    public String showCategory(HttpServletRequest request, Model model) {
        String password = request.getParameter("password");
        String name = request.getParameter("username");
        if(password.equals("123") && name.equals("seller")){
            model.addAttribute("msg","管理员登录成功");
            List<Category> categoryList=catalogService.getCategoryList();
            model.addAttribute("categoryList",categoryList);
            System.out.println("管理员登录成功");
            return "seller/showCategory";
        }else{
            System.out.println("管理员登录失败");
            model.addAttribute("msg","账号或密码错误");
            return "seller/sellerEntry";
        }
    }

    @GetMapping("/viewShowCategory")
    public String viewShowCategory(Model model){
        List<Category> categoryList=catalogService.getCategoryList();
        model.addAttribute("categoryList",categoryList);
        System.out.println("正在编辑商品大类");
        return "seller/showCategory";
    }

    @GetMapping("/viewShowProduct")
    public String viewShowProduct(String categoryId,Model model){
        List<Category> categoryList=catalogService.getCategoryList();
        model.addAttribute("categoryList",categoryList);
        System.out.println(categoryId);
        if(categoryId!=null){
            List<Product> productList = catalogService.getProductListByCategory(categoryId);
            model.addAttribute("productList",productList);
        }
        System.out.println("正在编辑商品小类");
        return "seller/showProduct";
    }

    @GetMapping("/viewShowItem")
    public String viewShowItem(String productId, Model model){
        System.out.println("正在编辑商品详细信息");
        if(productId!=null){
            System.out.println("查看小类"+productId);
            List<Item> itemList = catalogService.getItemListByProduct(productId);
            Product product1=catalogService.getProduct(productId);
            System.out.println("容量"+itemList.toString());
            model.addAttribute("itemList",itemList);
            model.addAttribute("productId",productId);
            model.addAttribute("categoryId",product1.getCategoryId());
            return "/seller/showItem";
        }else{
            Product product2=catalogService.getProduct(productId);
            String categoryId=product2.getCategoryId();
            return "viewShowProduct?categoryId="+categoryId;
        }
    }

    @RequestMapping(value="seller_searchProduct", method= RequestMethod.POST)
    private String doSellerSearchProducts(@RequestParam("keyword") String keyword, Model model) {
        if(keyword!=null) {
            List<Product> productList = catalogService.searchProductList(keyword);
            model.addAttribute("productList",productList);
            return "seller/showProduct";
        }
        return "seller/showCategory";
    }

    @GetMapping("/productAdd")
    public String productAdd(Model model){
        String msg=" ";
        model.addAttribute("msg",msg);
        return "seller/productAdd";
    }

    @RequestMapping(value="/productAdd", method= RequestMethod.POST)
    private String doProductAdd(@RequestParam("productid") String productid,@RequestParam("category") String category,@RequestParam("name") String name,@RequestParam("descn") String descn, Model model) {
        if(productid!=null && category!=null && name!=null && descn!=null) {
            catalogService.insertProduct(productid,category,name,descn);
            String msg="添加成功！";
            model.addAttribute("msg",msg);
            return "seller/productAdd";
        }else {
            String msg="各项值不能为空！";
            model.addAttribute("msg",msg);
            return "seller/productAdd";
        }
    }

    @GetMapping("/itemAdd")
    public String itemAdd(String productId,Model model){
        String msg=" ";
        model.addAttribute("msg",msg);
        model.addAttribute("productId",productId);
//        System.out.println(productId);
        return "seller/itemAdd";
    }

    @PostMapping("/itemAdd")
    public String itemAdd(HttpServletRequest request,Model model) {
        String itemid = request.getParameter("itemid");
        String productid = request.getParameter("productid");
        String listprice = request.getParameter("listprice");
        String unitcost = request.getParameter("unitcost");
        String attr1 = request.getParameter("attr1");
        if(itemid!=null &&productid!=null && listprice!=null && unitcost!=null && attr1!=null) {
            catalogService.insertItem(itemid,productid,listprice,unitcost,attr1);
            String msg="添加成功！";
            model.addAttribute("msg",msg);
            model.addAttribute("productId",productid);
            return "seller/itemAdd";
        }else {
            String msg="各项值不能为空！";
            model.addAttribute("msg",msg);
            model.addAttribute("productId",productid);
            return "seller/itemAdd";
        }
    }

    //跳转编辑
    @GetMapping("/viewItemEdit")
    public String viewItemEdit(String itemId, Model model){
        Item item=catalogService.getItem(itemId);
        model.addAttribute("item",item);
        return "/seller/itemEdit";
    }

    //编辑提交
    @PostMapping("/itemEdit")
    public String itemEdit(String itemId, HttpServletRequest request, Model model){
        try {
            Item item = catalogService.getItem(itemId);
            System.out.println(item.getItemId() + "正在编辑");
            String a1 = request.getParameter("attribute1");
            String a2 = request.getParameter("attribute2");
            String a3 = request.getParameter("attribute3");
            String a4 = request.getParameter("attribute4");
            String a5 = request.getParameter("attribute5");
            String s = request.getParameter("status");
            String su = request.getParameter("supplierId");
            String q = request.getParameter("quantity");
            System.out.println("商品" + item.product.getProductId());
            String str=item.product.getProductId();
            System.out.println(a1 + " a1 " +a2 + " a2 " + a3 + " a3 " + a4 + " a4  "+ a5+ " a5 "+ s + " s "+ su + " su "+ q +" q");
            if (a1 != null && a2 != null && a3 != null && a4 != null && a5 != null && s != null & su != null && q != null) {
                item.setQuantity(Integer.parseInt(q));
                item.setSupplierId(Integer.parseInt(s));
                item.setAttribute1(a1);
                item.setAttribute2(a2);
                item.setAttribute3(a3);
                item.setAttribute4(a4);
                item.setAttribute5(a5);
                item.setStatus(s);
                catalogService.updateItem(item);
                List<Item> itemList = catalogService.getItemListByProduct(str);
                Product product1=catalogService.getProduct(str);
                System.out.println("容量"+itemList.toString());
                model.addAttribute("itemList",itemList);
                model.addAttribute("productId",str);
                model.addAttribute("categoryId",product1.getCategoryId());
                return "/seller/showItem";
            } else {
                model.addAttribute("msg", "输入不能为空，编辑失败");
            }
            return "seller/viewShowCategory";
        }catch(Exception e){
            e.printStackTrace();
            model.addAttribute("msg", "输入不能为空，编辑失败");
            return "itemEdit?itemId="+itemId;
        }
    }

    //跳转编辑
    @GetMapping("/viewItemEdit")
    public String viewItemEdit(String itemId, Model model){
        Item item=catalogService.getItem(itemId);
        model.addAttribute("item",item);
        System.out.println("viewItem");
        return "/seller/itemEdit";
    }

    //编辑提交
    @PostMapping("/itemEdit")
    public String itemEdit(String itemId, HttpServletRequest request, Model model) {
        try {
            Item item = catalogService.getItem(itemId);
            System.out.println(item.getItemId() + "正在编辑");
            String a1 = request.getParameter("attribute1");
            String a2 = request.getParameter("attribute2");
            String a3 = request.getParameter("attribute3");
            String a4 = request.getParameter("attribute4");
            String a5 = request.getParameter("attribute5");
            String s = request.getParameter("status");
            String su = request.getParameter("supplierId");
            String q = request.getParameter("quantity");
            System.out.println("商品" + item.product.getProductId());
            String str = item.product.getProductId();
            System.out.println(a1 + " a1 " + a2 + " a2 " + a3 + " a3 " + a4 + " a4  " + a5 + " a5 " + s + " s " + su + " su " + q + " q");
            if (a1 != null && a2 != null && a3 != null && a4 != null && a5 != null && s != null & su != null && q != null) {
                item.setQuantity(Integer.parseInt(q));
                item.setSupplierId(Integer.parseInt(s));
                item.setAttribute1(a1);
                item.setAttribute2(a2);
                item.setAttribute3(a3);
                item.setAttribute4(a4);
                item.setAttribute5(a5);
                item.setStatus(s);
                catalogService.updateItem(item);
                List<Item> itemList = catalogService.getItemListByProduct(str);
                Product product1 = catalogService.getProduct(str);
                System.out.println("容量" + itemList.toString());
                model.addAttribute("itemList", itemList);
                model.addAttribute("productId", str);
                model.addAttribute("categoryId", product1.getCategoryId());
                return "/seller/showItem";
            } else {
                model.addAttribute("msg", "输入不能为空，编辑失败");
            }
            return "seller/viewShowCategory";
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("msg", "输入不能为空，编辑失败");
            return "itemEdit?itemId=" + itemId;
        }
    }




}
